using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using HelloWindowsPhoneMVC.Controllers;
using HelloWindowsPhoneMVC.Model;
using HelloWindowsPhoneMVC.Services;
using WindowsPhoneEssentials.Commands;
using WindowsPhoneMVC;

namespace HelloWindowsPhoneMVC.ViewModels.Home
{
    public class DebugPageViewModel : ViewModelBase
    {
        private readonly SomeClass parameter;
        private SomeClass parameterWritable;

        public DebugPageViewModel(SomeClass parameter, SomeClass parameterWritable)
        {
            this.parameter = parameter;
            this.parameterWritable = parameterWritable;
            Transient(() => ParameterWritable);
            ActionWithComplexParam = Controller<AnotherController>().InvokeCommand(c => c.ComplexArg(ParameterWritable));
            Items = new ObservableCollection<ItemFromList>
                        {
                            new ItemFromList { Title = "Item1" },
                            new ItemFromList { Title = "Item2" }
                        };
            //Use if you need to refresh some data, controller can call back to this view
            RefreshCommand = Controller<AnotherController>().InvokeWithResultCommand<string>(c => c.PerformSearch(Parameter), HandleCallback);
            GetValueCommand = Controller<DebugController>().InvokeWithResultCommand<string>(c => c.PageWithResult(), HandleCallback);
            NavigateWithPrivateCommand = new DelegateCommand(NavigateWithPrivate);
            InvokeBackgroundCommand = Controller<DebugController>().InvokeBackgroundCommand(c => c.BackgroundAction);
        }

        private void NavigateWithPrivate()
        {
            var local = new SomeClass();
            Controller<AnotherController>().Invoke(c=>c.PerformSearch(local));
        }

        private void HandleCallback(string message)
        {
            MessageBox.Show(message ?? "NULL", "Controller action result", MessageBoxButton.OK);
        }

        public ObservableCollection<ItemFromList> Items { get; private set; }

        public SomeClass Parameter
        {
            get { return parameter; }
        }

        public SomeClass ParameterWritable
        {
            get { return parameterWritable; }
            set
            {
                parameterWritable = value;
                OnPropertyChanged(() => ParameterWritable);
            }
        }

        public ICommand RefreshCommand { get; private set; }
        public ICommand ActionWithComplexParam { get; private set; }
        public ICommand GetValueCommand { get; private set; }
        public ICommand NavigateWithPrivateCommand { get; private set; }
        public ICommand InvokeBackgroundCommand { get; private set; }
    }
}